package com.lujieni.msg.consumer.controller;


import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/job")
public class JobController {


    /**
     * 1、简单任务示例（Bean模式）
     */
    @XxlJob("demoJobHandler")
    public ReturnT<String> demoJobHandler(String param) throws Exception {
        XxlJobLogger.log("msg-consumer");
        System.out.println("msg-consumer");
        for (int i = 0; i < 3; i++) {
            XxlJobLogger.log("beat at:" + i);
            TimeUnit.SECONDS.sleep(2);
        }
        return ReturnT.SUCCESS;
    }

    @XxlJob("bigTask")
    public ReturnT<String> bigTask(String param) throws Exception {
        XxlJobLogger.log("bigTask");
        System.out.println("bigTask");
        TimeUnit.MINUTES.sleep(1);
        return ReturnT.SUCCESS;
    }

    @XxlJob("paramTask")
    public ReturnT<String> paramTask(String param) throws Exception {
        XxlJobLogger.log("paramTask");
        // 解析参数 (可以使用工具类，例如：StringUtils.split)
        String[] params = param.split("&");
        String name = params[0].split("=")[1];
        int age = Integer.parseInt(params[1].split("=")[1]);
        XxlJobLogger.log("姓名：{}，年龄：{}", name, age);
        System.out.println("paramTask");
        return ReturnT.SUCCESS;
    }

}
